#include <IRremote.h>//导入红外库
int RECV_PIN = A3;//红外接收引脚
IRrecv irrecv(RECV_PIN); 
decode_results results;
float Value_cm;
bool qh = 1;  //初始布尔值为真
void setup() {
  //Serial.begin(9600);//串口输出调试
  irrecv.enableIRIn();
  pinMode(12, OUTPUT);//定义12号口为输出模式
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(A5, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(A1, OUTPUT);//超声波tr
  pinMode(A0, INPUT);//超声波ec
  digitalWrite(12, 0);//初始化12号口为低电平
  digitalWrite(13, 0);
  digitalWrite(2, 0);
  digitalWrite(3, 0);
  digitalWrite(4, 0);
  digitalWrite(5, 0);
  digitalWrite(6, 0);
  digitalWrite(7, 0);
  digitalWrite(8, 0);
  digitalWrite(9, 0);
  digitalWrite(10, 0);
  digitalWrite(11, 0);
  digitalWrite(A5, 0);
  digitalWrite(A4, 1);
  shi();  //调用shi自定义函数
  for (int i = 0 ; i < 50 ; i++) {
    buzzer();
  }
  qingling();//调用qingling自定义函数
}
void loop() {
  hong_wai();         //扫描红外 
  if (qh == 1) {        //判断qh是不是1
    digitalWrite(A5, 1);
    digitalWrite(A1, LOW); //低高低电平发一个短时间脉冲去TrigPin
    delayMicroseconds(2);  //延时2微妙
    digitalWrite(A1, HIGH);
    delayMicroseconds(10);
    digitalWrite(A1, LOW);
    Value_cm = float( pulseIn(A0, HIGH) * 17 ) / 1000; //将回波时间换算成cm
    Serial.print(Value_cm);   // 串口打印输出
    Serial.println("cm");
    delay(10);
    if (Value_cm < 200) {
      shi();
      loop();
    }
    else {
      qingling();
    }
  } else {    //否者执行
    qingling();       //熄灭全部led
    digitalWrite(A5, 0);
  }
}
void buzzer() {           //无源蜂鸣器控制
  for (int i = 0 ; i < 100 ; i++)   //循环100次
  {
    digitalWrite(A4, HIGH);   //设置输出高电平
    delayMicroseconds(100); //延时PotBuffer值 us
    digitalWrite(A4, LOW);    //设置输出低电平
    delayMicroseconds(100);       //延时100us
  }
}
void qingling() {         //熄灭全部
  digitalWrite(2, 0);
  digitalWrite(3, 0);
  digitalWrite(4, 0);
  digitalWrite(5, 0);
  digitalWrite(6, 0);
  digitalWrite(7, 0);
  digitalWrite(8, 0);
  digitalWrite(9, 0);
  digitalWrite(10, 0);
  digitalWrite(11, 0);
  digitalWrite(12, 0);
  digitalWrite(13, 0);
  // Serial.println("清零");
}
void hong_wai() {         //红外检测
  //红外接收检测
  if (irrecv.decode(&results)) { //检查是否接收到红外遥控信号
    int i = results.value;   //将红外信号赋值给常亮i
    Serial.println(i);      // 打印输出i
    switch (i) {
      case (-23971)://开
        buzzer();
        qh = 1;
        // Serial.println(F("KAI"));
        break;
      case (-7651)://关
        buzzer();
        qh = 0;
        //Serial.println(F("OFF"));
        break;
      case (-20401)://0
        buzzer();
        ling();
        break;
      case (12495)://1
        buzzer();
        yi();
        break;
      case (6375)://2
        buzzer();
        er();
        break;
      case (31365)://3
        buzzer();
        san();
        break;
      case (4335)://4
        buzzer();
        si();
        break;
      case (14535)://5
        buzzer();
        wu();
        break;
      case (23205)://6
        buzzer();
        liu();
        break;
      case (17085)://7
        buzzer();
        qi();
        break;
      case (19125)://8
        buzzer();
        ba();
        break;
      case (21165)://9
        buzzer();
        jiu();
        break;
      case (25245)://10
        buzzer();
        shi();
        delay(1000);
        break;
      case (8925)://CH-   1314
        buzzer();
        chushi();        //过度动画
        yi();            //1
        chushi();        //过度动画
        san();           //3
        chushi();        //过度动画
        yi();            //1
        chushi();        //过度动画
        si();            //4
        break;
      case (765)://CH+     521
        buzzer();
        wu();
        chushi();
        er();
        chushi();
        yi();    ;
        break;
      case (-8161)://摩斯电码   点
        buzzer();
        dian();
        break;
      case (-22441)://摩斯电码   杠
        buzzer();
        gang();
        break;
      case (-28561)://摩斯电码    结束
        buzzer();
        jieshu();
        break;
      case (26775): //摩斯电码sos
        buzzer();
        int tim = 500;
        dian();
        delay(tim);
        dian();
        delay(tim);
        dian();
        delay(tim);
        jieshu();
        delay(tim);
        gang();
        delay(tim);
        gang();
        delay(tim);
        gang();
        delay(tim);
        jieshu();
        delay(tim);
        dian();
        delay(tim);
        dian();
        delay(tim);
        dian();
        delay(tim);
        jieshu();
        //Serial.println(F("打印sos"));
        break;
    } if (i == -26521) {//摩斯电码   廖张雨520
      buzzer();
      int shijian = 600;
      dian();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      jieshu();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      jieshu();
      delay(shijian);
      gang();
      delay(shijian);
      dian();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      jieshu();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      jieshu();
      delay(shijian);
      dian();
      delay(shijian);
      dian();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      jieshu();
      delay(shijian);
      dian();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      gang();
      delay(shijian);
      jieshu();
      delay(shijian);
      // Serial.println(F("打印lzy521"));
    }
    irrecv.resume();
  }
}
void shi() {              //10—检测是否有损坏的灯珠
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(5, 1);
  delay(30);
  digitalWrite(6, 1);
  delay(30);
  digitalWrite(7, 1);
  delay(30);
  digitalWrite(8, 1);
  delay(30);
  digitalWrite(9, 1);
  delay(30);
  digitalWrite(10, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
}
void jiu() {              //9
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(4, 0);
  delay(30);
  digitalWrite(5, 1);
  delay(30);
  digitalWrite(5, 0);
  delay(30);
  digitalWrite(6, 1);
  delay(30);
  digitalWrite(6, 0);
  delay(30);
  digitalWrite(7, 1);
  delay(30);
  digitalWrite(7, 0);
  delay(30);
  digitalWrite(8, 1);
  delay(30);
  digitalWrite(8, 0);
  delay(30);
  digitalWrite(9, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void ba() {               //8
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(4, 0);
  delay(30);
  digitalWrite(5, 1);
  delay(30);
  digitalWrite(5, 0);
  delay(30);
  digitalWrite(6, 1);
  delay(30);
  digitalWrite(6, 0);
  delay(30);
  digitalWrite(7, 1);
  delay(30);
  digitalWrite(7, 0);
  delay(30);
  digitalWrite(8, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void qi() {               //7
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(4, 0);
  delay(30);
  digitalWrite(5, 1);
  delay(30);
  digitalWrite(5, 0);
  delay(30);
  digitalWrite(6, 1);
  delay(30);
  digitalWrite(6, 0);
  delay(30);
  digitalWrite(7, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void liu() {              //6
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(4, 0);
  delay(30);
  digitalWrite(5, 1);
  delay(30);
  digitalWrite(5, 0);
  delay(30);
  digitalWrite(6, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void wu() {               //5
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(30);
  digitalWrite(4, 0);
  delay(30);
  digitalWrite(5, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void si() {               //4
  digitalWrite(12, 1);//12号引脚高电平
  delay(30);//延时30毫秒
  digitalWrite(12, 0);//12号引脚低电平
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(30);
  digitalWrite(3, 0);
  delay(30);
  digitalWrite(4, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void san() {              //3
  digitalWrite(12, 1);
  delay(30);
  digitalWrite(12, 0);
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(30);
  digitalWrite(2, 0);
  delay(30);
  digitalWrite(3, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void er() {               //2
  digitalWrite(12, 1);
  delay(30);
  digitalWrite(12, 0);
  delay(30);
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(30);
  digitalWrite(2, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void yi() {               //1
  digitalWrite(12, 1);
  delay(30);
  digitalWrite(12, 0);
  delay(30);
  digitalWrite(13, 1);
  delay(50);
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void ling() {             //0
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(2000);
}
void chushi() {           //过度动画
  byte tim = 30;
  digitalWrite(12, 1);
  delay(tim);
  digitalWrite(12, 0);
  delay(tim);
  digitalWrite(13, 1);
  delay(tim);
  digitalWrite(13, 0);
  delay(tim);
  digitalWrite(2, 1);
  delay(tim);
  digitalWrite(2, 0);
  delay(tim);
  digitalWrite(3, 1);
  delay(tim);
  digitalWrite(3, 0);
  delay(tim);
  digitalWrite(4, 1);
  delay(tim);
  digitalWrite(4, 0);
  delay(tim);
  digitalWrite(5, 1);
  delay(tim);
  digitalWrite(5, 0);
  delay(tim);
  digitalWrite(6, 1);
  delay(tim);
  digitalWrite(6, 0);
  delay(tim);
  digitalWrite(7, 1);
  delay(tim);
  digitalWrite(7, 0);
  delay(tim);
  digitalWrite(8, 1);
  delay(tim);
  digitalWrite(8, 0);
  delay(tim);
  digitalWrite(9, 1);
  delay(tim);
  digitalWrite(9, 0);
  delay(tim);
  digitalWrite(10, 1);
  delay(tim);
  digitalWrite(10, 0);
  delay(tim);
  digitalWrite(11, 1);
  delay(tim);
  digitalWrite(11, 0);
  delay(tim);
}
void dian() {             //莫斯电码   点
  digitalWrite(13, 1);
  delay(100);
  digitalWrite(13, 0);
}
void gang() {             //莫斯电码   杠
  digitalWrite(13, 1);
  digitalWrite(2, 1);
  digitalWrite(3, 1);
  delay(100);
  digitalWrite(13, 0);
  digitalWrite(2, 0);
  digitalWrite(3, 0);
}
void jieshu() {           //莫斯电码  停顿
  digitalWrite(11, 1);
  digitalWrite(12, 1);
  delay(100);
  digitalWrite(11, 0);
  digitalWrite(12, 0);
}
